package LoginTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-22
 * Time: 20:40
 */
//登陆页
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        //判断用户名和密码是否完整
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        if(userName==null || userName.equals("") || password == null || password.equals("")){
            resp.getWriter().write("用户名或密码不完整!!!");
            return;
        }
        //判断用户名和密码是否正确
        if(!userName.equals("shifangxu") || !password.equals("123456")){
            resp.getWriter().write("用户名或密码错误!!!");
            return;
        }
        //如果session对象不存在就创建出一个session对象
        HttpSession session = req.getSession(true);
        //加入键值对
        session.setAttribute("userName","shifangxu");
        //跳转到主页
        resp.sendRedirect("index");
    }
}
